package id.co.rho.scoring.cid.client.widgets;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.google.gson.JsonArray;
import com.google.gson.JsonPrimitive;
import com.google.gwt.core.client.GWT;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.Hidden;

public class FormExportFItoExcel extends FormPanel {

	Hidden mapOfGrip;
	Hidden jsonarray;
	Hidden fileName;
	String namaFile;
	JSONArray array = new JSONArray();
	Map<String, JSONArray[]> map = new TreeMap<String, JSONArray[]>();

	public Map<String, JSONArray[]> getMap() {
		return map;
	}

	public void setarray(JSONArray array) {
		this.array = array;
	}
	
	
	
	public void setMap(Map<String, JSONArray[]> map) {
		this.map = map;
	}

	public FormExportFItoExcel() {
		super();
		setAction(GWT.getModuleBaseURL() + "exportFI");
		setMethod(Method.POST);
		mapOfGrip = new Hidden("mapOfGrip");
		add(mapOfGrip);
		jsonarray = new Hidden("jsonarray");
		add(jsonarray);
		fileName = new Hidden("fileName");
		add(fileName);
		setPadding(0);
	}

	public void sendToExporter() {

		Iterator<Map.Entry<String, JSONArray[]>> entries = map.entrySet()
				.iterator();
		JSONObject jsonObject = new JSONObject();
		while (entries.hasNext()) {
			Map.Entry<String, JSONArray[]> entry = entries.next();
			String key = entry.getKey();
			JSONArray jsonArray = new JSONArray();
			for (int i = 0; i < entry.getValue().length; i++) {
				jsonArray.set(i, entry.getValue()[i]);
			}
			jsonObject.put(key, jsonArray);
		}
		mapOfGrip.setValue(jsonObject.toString());
		jsonarray.setValue(array.toString());
		fileName.setValue(namaFile);
		submit();
	}

	public void setFileName(String fileName) {
		this.namaFile = fileName;
	}
}
